#dialog 的意思:弹窗,编辑窗口
from PyQt6.QtCore import Qt
from PyQt6.QtWidgets import QGridLayout
from qfluentwidgets import MessageDialog, LineEdit, ComboBox, StrongBodyLabel, MessageBoxBase, SubtitleLabel


class BaseStudentDialog(MessageBoxBase):
    def __init__(self, title, parent=None):
        #title: str 这应该指的是参数的数据类型
        super().__init__(parent)
        self.title= title #就是窗口的名称
        self.setWindowTitle(self.title)
        self.setup_ui() #设置界面
        #self.setButtonText(MessageDialog.Ok, "确定")
        #self.setButtonText(MessageDialog.Cancel, "取消")
        #self.setButtonText(MessageDialog.Abort, "终止")
        #self.setButtonText(MessageDialog.Retry, "重试")
        #self.setButtonText(MessageDialog.Ignore, "忽略")
        #self.setButtonText(MessageDialog.Yes, "是")
    def setup_ui(self):
        self.titleLabel=SubtitleLabel(self.title,self)
        self.viewLayout.addWidget(self.titleLabel)
        grid_layout = QGridLayout(self)#创建一个网格布局,后面会往这个网格布局里添加控件
        self.viewLayout.addLayout(grid_layout)#把界面布局添加到网格布局中
        #开始书写grid_layout的代码
        self.nameInput=LineEdit(self)
        self.numberInput=LineEdit(self)
        self.genderCombo=ComboBox(self) #ComboBox 是一个下拉框
        self.genderCombo.addItems(["男","女"]) #添加元素
        self.classCombo=ComboBox(self) #ComboBox 是一个下拉框
        self.chineseScoreInput=LineEdit(self)
        self.mathScoreInput=LineEdit(self)
        self.englishScoreInput=LineEdit(self)


        #添加字段到网格布局
        fileds=[
            ("姓名",self.nameInput),
            ("学号",self.numberInput),
            ("性别",self.genderCombo),
            ("班级",self.classCombo),
            ("语文成绩",self.chineseScoreInput),
            ("数学成绩",self.mathScoreInput),
            ("英语成绩",self.englishScoreInput)
        ]
        for index,field in enumerate(fileds):
            label_text,widget=field #元组拆包
            #widget.setMinimumWidth(200) #设置最小宽度
            label=StrongBodyLabel(label_text,self)#创建一个强体标签
            grid_layout.addWidget(label,index,0)#控件,行,列 #往网格布局中添加控件
            grid_layout.addWidget(widget,index,1)
        grid_layout.setColumnStretch(1,1)#设置列拉伸,这样整体变宽时,列也会自动拉伸
        grid_layout.setAlignment(Qt.AlignmentFlag.AlignLeft)#设置左对齐
        #重新定义ok 和取消的文本是 确认 取消
        self.yesButton.setText("确定")
        self.cancelButton.setText("取消")
        #设置输入框的宽度
        for widget in [self.nameInput,self.numberInput,self.chineseScoreInput,self.mathScoreInput,self.englishScoreInput]:
           widget.setFixedWidth(200)
        self.nameInput.setFocus()#将光标放到name输入框
        self.yesButton.setFocus(False)#将确认按钮取消焦点
        self.cancelButton.setFocus(False)
        #好了,这就创建好了弹窗的基类
        #接下来就是建立对象了,让add和edit继承这个类
class AddStudentDialog(BaseStudentDialog):
    def __init__(self, parent=None):
        super().__init__("添加学生", parent)
        self.student_id=None#添加时,不需要有学生id,所以设置为None
        self.yesButton.setText("添加")#设置按钮的文本
class EditStudentDialog(BaseStudentDialog):
    def __init__(self, parent=None):
        super().__init__("编辑学生信息", parent)
        #self.setup_ui()
        #设置按钮的文本
        self.yesButton.setText("修改")
        





